<template>
  <div class="app-container">
    <div class="operate-btn-container">
      <el-button type="primary" @click="add">添加</el-button>
    </div>
    <el-form :inline="true" :model="recordInfo.queryForm" class="demo-form-inline">
      <el-form-item label="权益卡编号">
        <el-input v-model="recordInfo.queryForm.benefitCardId" placeholder="权益卡编号" clearable />
      </el-form-item>
      <el-form-item label="权益卡名称">
        <el-input v-model="recordInfo.queryForm.storeName" placeholder="权益卡名称" clearable />
      </el-form-item>
      <el-form-item label="启用状态">
        <el-select v-model="recordInfo.queryForm.st" clearable style="width:120px">
          <el-option
            v-for="(item,index) in benefitCardStateList"
            :key="index"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="createTimeValue"
          unlink-panels
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <div style="display: flex;justify-content: space-between;">
      <h5>权益卡列表</h5>
      <div class="statistics" />
    </div>
    <el-table
      ref="multipleTable"
      v-loading="recordInfo.loading"
      class="table-default"
      highlight-current-row
      :data="recordInfo.list"
    >
      <el-table-column prop="benefCardId" label="权益卡编号" width="169" align="center" />
      <el-table-column prop="benefCardNm" label="权益卡名称" align="center" />
      <el-table-column prop="cardValidType" label="卡片有效时间" width="180" align="center" />
      <el-table-column prop="cardGrantNum" label="卡片发放数量" width="120" align="center" />
      <el-table-column prop="createUserName" label="创建人" width="80" align="center" />
      <el-table-column prop="crtTm" label="创建时间" width="140" align="center" />
      <el-table-column prop="st" label="启用状态" width="110" align="center" />
      <el-table-column label="操作" show-tooltip-when-overflow fixed="right" width="60" align="center">
        <template v-slot="scope">
          <div class="list-operate-menu">
            <el-tooltip class="item" effect="dark" content="查看" placement="top">
              <i
                class="el-icon-document"
                @click="goDetail(scope.row)"
              />
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="recordInfo.total > 0"
      :total="recordInfo.total"
      :page.sync="recordInfo.queryForm.page"
      :limit.sync="recordInfo.queryForm.pageSize"
      @pagination="getRecordList"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import filters from '@/filters/index'
import ListMixin from '@/mixins/modules/list'
import messageMixin from '@/mixins/modules/message'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: { ...filters },
  mixins: [ListMixin, messageMixin],
  data() {
    return {
      autoGetList: false,
      searchOrganizationLoading: false,
      organizationList: [],
      createTimeValue: null,
      recordInfo: {
        queryForm: {
          page: 1,
          pageSize: 5,
          returnCount: true,
          orderBy: 'createTimeDesc',
          searchContent: '',
          createTimeStart: '',
          createTimeEnd: '',
          notSubmit: {
            searchOrganizationId: '',
            organizationNameSearchOrganizationId: '',
            supplierOrganizationNameSearchSupplierOrganizationId: '',
            searchSupplierOrganizationId: ''
          }
        },
        requestUrl: this.$api.getBenefitCardList,
        list: [],
        total: 0
      },
      beforeRequest() {
        this.recordInfo.queryForm.returnCount = this.recordInfo.queryForm.page === 1
        this.recordInfo.queryForm.createTimeStart = this.createTimeValue !== null ? this.createTimeValue[0].getTime() : undefined
        this.recordInfo.queryForm.createTimeEnd = this.createTimeValue !== null ? this.createTimeValue[1].getTime() + 86399999 : undefined
      },
      getListCallback(res) {
        if (undefined !== res.list && res.list.length > 0) {
          let item
          for (let i = 0; i < res.list.length; i++) {
            item = res.list[i]
            res.list[i].crtTm = this.$commonUtils.getShowTime(res.list[i].crtTm)
            for (let j = 0; j < this.benefitCardStateList.length; j++) {
              if (this.benefitCardStateList[j].value === res.list[i].st) {
                res.list[i].st = this.benefitCardStateList[j].name
                break
              }
            }
            if (item.cardGrantNum === 0) {
              item.cardGrantNum = '不限制'
            }
            if (item.cardValidType === 2) {
              item.cardValidType = this.$commonUtils.getShowDate(res.list[i].validStartTm) + ' - ' + this.$commonUtils.getShowDate(res.list[i].validEndTm)
            } else if (item.cardValidType === 1) {
              item.cardValidType = '领取后' + res.list[i].validDays + '天内有效'
            } else {
              item.cardValidType = '永久有效'
            }
          }
        }
      }
    }
  },
  computed: {
    ...mapGetters([
      'userInfo',
      'benefitCardStateList'
    ])
  },
  created() {
    const query = this.$route.query
    if (undefined !== query) {
      try {
        const params = JSON.parse(decodeURIComponent(this.$Base64.decode(query.params)))
        if (undefined !== params) {
          console.log(params)
        }
      } catch (e) {
        // nothing
      }
    }
    this.getRecordList()
  },
  methods: {
    clearOrganization() {
      this.organizationList = []
    },
    searchOrganization(query) {
      this.searchOrganizationLoading = true
      this.$api.getOrganizationList({ returnCount: true, page: 1, pageSize: 10, organizationName: query }, undefined).then(res => {
        this.searchOrganizationLoading = false
        this.organizationList = res.list
      })
    },
    init() {
      this.recordInfo.list = []
      this.getRecordList()
      this.multipleSelection = []
    },
    search() {
      this.recordInfo.queryForm.page = 1
      this.recordInfo.list = []
      this.getRecordList()
    },
    goDetail(item) {
      this.$addTab('memberBenefitCardForm', {
        benefCardId: item.benefCardId
      }, '修改权益卡 - ' + item.benefCardNm)
    },
    add() {
      this.$addTab('memberBenefitCardForm', {}, '添加权益卡')
    }
  }
}
</script>

<style lang="scss" scoped>
.statistics {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
  margin-top: 20px;
  font-size: 14px;

  .statistics-item {
    margin-left: 10px;
  }

  .value {
    color: #F07505;
    font-weight: bold;
  }
}
</style>
